/*
 * Copyright 2013 HVTTGROUP
 * Tool Genneric code by hvttgroup
 * @version v1.0
 * @see http://hvttgroup.com
 *
 */
package com.hvtt.web.models;

import com.hvtt.web.connection.Command;
import com.hvtt.web.connection.WEBConnection;
import com.hvtt.web.connection.WEBParamCollection;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

/**
 *
 * @author Nguyen Hoang Danh
 */
public class Languages implements Serializable{

// <editor-fold defaultstate="collapsed" desc="Generic code properties for class">
    private String languagesID = "";
    private String languagesName = "";
    private String images = "";
    private boolean languagesDefault = false;
    private boolean active = false;
    private int order = 0;
    private String dienGiai = "";
    private WEBConnection webConnection;
    private static String STORE_NAME = "{call proc_languages(?,?,?,?,?,?,?,?)}";
    private static String TABLE_NAME = "languages";
    private static final String key = "LanguagesID";
//</editor-fold>

// <editor-fold defaultstate="collapsed" desc="Generic code Constructor class">
    public Languages() {
        webConnection = WEBConnection.getInstance();
    }

    public Languages(String languagesID, String languagesName, String images, boolean languagesDefault, boolean active, int order, String dienGiai) {
        this.languagesID = languagesID;
        this.languagesName = languagesName;
        this.images = images;
        this.languagesDefault = languagesDefault;
        this.active = active;
        this.order = order;
        this.dienGiai = dienGiai;
        webConnection = WEBConnection.getInstance();
    }

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code init param">
    private WEBParamCollection fillParamsAndAction(String action) {
        WEBParamCollection params = WEBParamCollection.getInstance();
        params.add(action);
        params.add(this.getLanguagesID());
        params.add(this.getLanguagesName());
        params.add(this.getImages());
        params.add(this.getLanguagesDefault());
        params.add(this.getActive());
        params.add(this.getOrder());
        params.add(this.getDienGiai());
        return params;
    }

    private WEBParamCollection fillParamsAndAction(String action, Languages languages1) {
        WEBParamCollection params = WEBParamCollection.getInstance();
        params.add(action);
        params.add(languages1.getLanguagesID());
        params.add(languages1.getLanguagesName());
        params.add(languages1.getImages());
        params.add(languages1.getLanguagesDefault());
        params.add(languages1.getActive());
        params.add(languages1.getOrder());
        params.add(languages1.getDienGiai());
        return params;
    }

// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code event">
    public boolean insert(Languages varLanguages) {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Add", varLanguages));
        return bExcute;
    }

    public boolean save() {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Add"));
        return bExcute;
    }

    public boolean update(Languages varLanguages) {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Update", varLanguages));
        return bExcute;
    }

    public boolean update() {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Update"));
        return bExcute;
    }

    public boolean remove(String key) {
        this.setLanguagesID(key);
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Delete"));
        return bExcute;
    }

    public boolean remove(List<Languages> listLanguages) {
        WEBParamCollection p = WEBParamCollection.getInstance();
        boolean result = false;
        String sql = "delete from " + TABLE_NAME + " where " + TABLE_NAME + "." + key + " in (";
        for (int i = 0; i < listLanguages.size(); i++) {
            sql = sql + "?,";
            p.add(listLanguages.get(i).getLanguagesID());
            if (i % 900 == 0) {
                sql = sql.substring(0, sql.lastIndexOf(",")) + ")";
                result = webConnection.executeUpdate(sql, Command.sqlText, p);
                p.clear();
                sql = "delete from " + TABLE_NAME + " where " + TABLE_NAME + "." + key + " in (";
            }
        }
        if (sql.lastIndexOf(",") != -1) {
            sql = sql.substring(0, sql.lastIndexOf(",")) + ")";
            result = webConnection.executeUpdate(sql, Command.sqlText, p);
        }
        return result;
    }

    public boolean removeAll() {
        WEBParamCollection p = WEBParamCollection.getInstance();
        boolean bExcute = webConnection.executeUpdate("delete from " + TABLE_NAME, Command.sqlText, p);
        return bExcute;
    }

    public Languages findByKey(String key) {
        Languages varLanguages1 = null;

        try {
            this.setLanguagesID(key);

            List<Languages> result = webConnection.executeQuery(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("GetByKey"), Languages.class);
            if (!result.isEmpty()) {
                varLanguages1 = result.get(0);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Languages.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
        return varLanguages1;
    }

    public List<Languages> findAll() {
        List<Languages> lstLanguages = null;
        try {
            lstLanguages = webConnection.executeQuery(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("GetAll"), Languages.class);
        } catch (SQLException ex) {
            Logger.getLogger(Languages.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
        return lstLanguages == null ? new ArrayList<Languages>() : lstLanguages;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Generic code getter and setter for properties of class">
    public String getLanguagesID() {
        return languagesID;
    }

    public String getLanguagesName() {
        return languagesName;
    }

    public String getImages() {
        return images;
    }

    public boolean getLanguagesDefault() {
        return languagesDefault;
    }

    public boolean getActive() {
        return active;
    }

    public int getOrder() {
        return order;
    }

    public String getDienGiai() {
        return dienGiai;
    }

    public void setLanguagesID(String languagesID) {
        this.languagesID = languagesID;
    }

    public void setLanguagesName(String languagesName) {
        this.languagesName = languagesName;
    }

    public void setImages(String images) {
        this.images = images;
    }

    public void setLanguagesDefault(boolean languagesDefault) {
        this.languagesDefault = languagesDefault;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public void setDienGiai(String dienGiai) {
        this.dienGiai = dienGiai;
    }

    public void resetAttribute() {
        languagesID = "";
        languagesName = "";
        images = "";
        languagesDefault = false;
        active = false;
        order = 0;
        dienGiai = "";
    }

    public void setWebConnection(WEBConnection webConnection) {
        this.webConnection = webConnection;
    }

    public WEBConnection getWebConnection() {
        return webConnection;
    }

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code hasCode and equals method">
    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(this.languagesID).append(this.languagesName).toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj instanceof Languages) {
            Languages otherObj = (Languages) obj;
            return new EqualsBuilder().append(this.languagesID, otherObj.languagesID).append(this.languagesName, otherObj.languagesName).isEquals();
        }
        return false;
    }

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code filterFields">
    public static Field[] filterFields() {
        try {
            Class c = Languages.class;
            return new Field[]{c.getDeclaredField("languagesID"), c.getDeclaredField("languagesName"), c.getDeclaredField("order"), c.getDeclaredField("dienGiai")};
        } catch (NoSuchFieldException | SecurityException ex) {
            Logger.getLogger(Languages.class.getName()).log(Level.SEVERE, null, ex);
        }
        return new Field[]{};
    }

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code other method">
    public Languages newInstance() {
        try {
            return this.getClass().newInstance();
        } catch (InstantiationException | IllegalAccessException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        }
        return new Languages();
    }

    /**
     * Copy properties form Languages to this
     *
     * @param languages1
     * @return Languages
     */
    public Languages newInstance(Languages languages1) {
        this.setLanguagesID(languages1.getLanguagesID());
        this.setLanguagesName(languages1.getLanguagesName());
        this.setImages(languages1.getImages());
        this.setLanguagesDefault(languages1.getLanguagesDefault());
        this.setActive(languages1.getActive());
        this.setOrder(languages1.getOrder());
        this.setDienGiai(languages1.getDienGiai());
        return this;
    }
//</editor-fold>
}
